package applicationPlugin.Editor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import interfacesplugins.InterfaceFilesTraitement;

public class VisionneurTexte extends JFrame implements InterfaceFilesTraitement {
	
	private File file;
	private JScrollPane jsp=new JScrollPane();
	private JScrollPane sp;
	private JPanel  iconCadre;
	private JTextArea l;
	private JButton b;
	boolean etat;
	
	public VisionneurTexte() {
		etat=false;
		b=new JButton();
		l=new JTextArea();
		iconCadre = new JPanel();
		iconCadre.setVisible(true);
		sp = new JScrollPane();
		sp.getViewport();
		add(sp);
		setTitle("VisonneurTexte");
		  setSize(600, 500);
	      setLocation(200, 100);
	}
	

	@Override
	public String getVersion() {
		// TODO Auto-generated method stub
		return "v1.0";
	}
	
	public JScrollPane buildText(File file) {
		b.setText(file.getAbsolutePath());
		l.setVisible(true);
		l.setText(this.readText(file));
		iconCadre.add(l);
		iconCadre.revalidate();
		iconCadre.setVisible(true);
		sp.getViewport().add(iconCadre);
		return sp;
	}
	
	public String readText(File file){
		BufferedReader lecteurAvecBuffer = null;
	    String ligne;
	    StringBuffer fichier = new StringBuffer();

	    try
	      {
			lecteurAvecBuffer = new BufferedReader(new FileReader(file));
			while((ligne = lecteurAvecBuffer.readLine()) != null){
				fichier.append(ligne);
				fichier.append("\n");			
			}
			lecteurAvecBuffer.close();
	      }
	    catch(FileNotFoundException exc){
	    	System.out.println("Erreur d'ouverture");
	      } 
	    catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		  }
	    
	    return fichier.toString();    
		
	}

	@Override
	public String getNomPlugin() {
		// TODO Auto-generated method stub
		return "Visionneur de texte";
	}

	@Override
	public String getDeveloppeur() {
		// TODO Auto-generated method stub
		return "CERF Rolland";
	}

	@Override
	public String[] getExtension() {
		// TODO Auto-generated method stub
		String[] ext = {"txt"};
		return ext;
	}

	@Override
	public boolean getEtat() {
		// TODO Auto-generated method stub
		return etat;
	}

	@Override
	public boolean setEtat(boolean etat) {
		// TODO Auto-generated method stub
		return this.etat=etat;
	}

	@Override
	public JFrame lancerFichier(File f) {
		// TODO Auto-generated method stub
		sp=buildText(f);
		VisionneurTexte v=new VisionneurTexte();
		v.add(sp);
		v.setVisible(true);
		return v;
		
		
	}

	@Override
	public ImageIcon getIcon() {
		// TODO Auto-generated method stub
		return new ImageIcon("../Plugins/bin/applicationPlugin/Editor/visionneurTexte.png");
	}

}

